package cn.gogpay.duoim.sdk.service.v3;

import cn.gogpay.duoim.sdk.model.IMRespResult;
import cn.gogpay.duoim.sdk.model.v3.base.IMCertificate;
import cn.gogpay.duoim.sdk.model.v3.msg.req.*;
import cn.gogpay.duoim.sdk.model.v3.msg.resp.*;
import cn.gogpay.duoim.sdk.service.IMApiFactory;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import okhttp3.Request;

public class IMMessageV3Service {

    /**
     * 获取最小最大seq（包括用户的，以及指定群组的）
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<GetMaxSeqResp> getSeq(IMCertificate token, GetMaxSeqReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/newest_seq");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<GetMaxSeqResp>> typeReference = new TypeReference<IMRespResult<GetMaxSeqResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 搜索消息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<SearchMessageResp> searchMsg(IMCertificate token, SearchMessageReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/search_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<SearchMessageResp>> typeReference = new TypeReference<IMRespResult<SearchMessageResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 发送消息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<SendMsgResp> sendMessage(IMCertificate token, SendMsgReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/send_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<SendMsgResp>> typeReference = new TypeReference<IMRespResult<SendMsgResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 批量发送消息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<BatchSendMsgResp> batchSendMsg(IMCertificate token, BatchSendMsgReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/batch_send_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<BatchSendMsgResp>> typeReference = new TypeReference<IMRespResult<BatchSendMsgResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 发送业务通知消息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<SendMsgResp> sendBusinessNotification(IMCertificate token, SendBusinessNotificationReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/send_business_notification");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<SendMsgResp>> typeReference = new TypeReference<IMRespResult<SendMsgResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 拉取历史消息（包括用户的，以及指定群组的）
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<PullMessageBySeqsResp> pullMsgBySeqs(IMCertificate token, PullMessageBySeqsReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/pull_msg_by_seq");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<PullMessageBySeqsResp>> typeReference = new TypeReference<IMRespResult<PullMessageBySeqsResp>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 撤回消息
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> revokeMsg(IMCertificate token, RevokeMsgReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/revoke_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * markMsgsAsRead
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> markMsgsAsRead(IMCertificate token, MarkMsgsAsReadReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/mark_msgs_as_read");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * markConversationAsRead
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> markConversationAsRead(IMCertificate token, MarkConversationAsReadReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/mark_conversation_as_read");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * getConversationsHasReadAndMaxSeq
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> getConversationsHasReadAndMaxSeq(IMCertificate token, GetConversationsHasReadAndMaxSeqReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/get_conversations_has_read_and_max_seq");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * setConversationHasReadSeq
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> setConversationHasReadSeq(IMCertificate token, SetConversationHasReadSeqReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/set_conversation_has_read_seq");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 全量清空指定会话消息 重置min seq 比最大seq大1
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> clearConversationsMsg(IMCertificate token, ClearConversationsMsgReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/clear_conversation_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }

    /**
     * 删除用户全部消息 重置min seq 比最大seq大1
     *
     * @param token 凭据
     * @param req   请求
     * @return 响应
     */
    public IMRespResult<String> userClearAllMsg(IMCertificate token, ClearConversationsMsgReq req) {
        String param = JSONUtil.toJsonStr(req);
        Request request = IMApiFactory.buildCertifiedRequest(token, param, "/msg/user_clear_all_msg");
        String data = IMApiFactory.callRequest(request);
        TypeReference<IMRespResult<String>> typeReference = new TypeReference<IMRespResult<String>>() {
        };
        return JSONUtil.toBean(data, typeReference, false);
    }
}
